package com.gsdse.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.gsdse.pojo.ReturnData;
import com.gsdse.service.UpLoadService;
import com.gsdse.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Service
public class UploadServiceImpl implements UpLoadService {


    @Override
    public ReturnData uploadPic(MultipartFile file, String departmentId) {

        //注意!!此处修改，对应的发送邮箱代码的获取图片路径也需要更改
        //String uploadPath = "/www/wwwroot/kj_recruit/pic";
        String uploadPath = "/usr/local/src/pic/";
        //String uploadPath = "D:\\KJpic";

        //String onlinePath = "https://tomcat.gsdkjwy.cn/pic/";
        //String onlinePath = "https://studynest.top/pic/";
        //String onlinePath = "https://gsdse.cn/pic/";

        File file1 = new File(uploadPath);
        if (!file1.exists()&&!file1.isDirectory()) {
            file1.mkdirs();
        }

        String originalFilename = file.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = departmentId + suffixName;

        File uploadFile = new File(file1, newFileName);

        ReturnData returnData = new ReturnData();
        try {
            String OnlinePath = AliOSSUtils.uploadFile(file,newFileName,"pic");
            file.transferTo(uploadFile);
            returnData.setCode(0);
            returnData.setMsg("上传文件成功！");
            returnData.setData(OnlinePath);
            return returnData;
        } catch (IOException | ClientException e) {
            log.error("上传文件错误！");
            e.printStackTrace();
            returnData.setMsg("文件上传失败！");
            returnData.setCode(1);
            return returnData;
        }



    }
}
